package com.service.seller;

import com.dao.seller.SellerLoginDao;
import com.po.Buyerinfo;
import com.po.Sellerinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
@Service("sellerService")
@Transactional
public class SellerServiceImpl implements SellerService{
    @Autowired
    private SellerLoginDao sellerDao;
//处理seller的登录的service层
    @Override
    public String login(Sellerinfo seller, Model model, HttpSession session) {
        if(sellerDao.login(seller) != null && sellerDao.login(seller).size() > 0) {
            //验证了用户名不为空且返回的list集合长度不为空。
            session.setAttribute("seller", seller);
            //添加商品与修改商品页面使用
            return "seller/main";
        }
        model.addAttribute("msg", "用户名或密码错误！");//传递数据给view
        //暴露为模型数据
        return "seller/login";
    }
    public String register(Sellerinfo suser, Model model, HttpSession session, String code) {
        if(!code.equalsIgnoreCase(session.getAttribute("code").toString())) {
            model.addAttribute("codeError", "验证码错误！");
            return "before/register";
        }
        int n = sellerDao.register(suser);
        if(n > 0) {
            return "before/login";
        }else {
            model.addAttribute("msg", "注册失败！");
            return "before/register";
        }
    }
    }

